package com.fc.redis.service;

import com.fc.redis.entity.TbUser;
import com.fc.redis.mapper.TbUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created By gao_e on 2020/5/8 14:34
 */
@Service
public class TestService {
    static Map<String, IService> map = new HashMap<>();
    // 类加载 的 时机 是在 IOC 初始化 之前，
    // 所以不能用 static 操作 IOC中对象！
//    static {
//        map.put("A", new AService()); // 会有问题
//    }

    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    public void init() {
        AService aService = (AService) applicationContext.getBean("aService");
        BService bService = (BService) applicationContext.getBean("bService");
        map.put("A",aService);
        map.put("B",bService);
    }

    @Autowired
    private TbUserMapper tbUserMapper;

    @Resource(name = "aService")
    private AService aService;

    public TbUserMapper getTbUserMapper() {
        return tbUserMapper;
    }

    public void setTbUserMapper(TbUserMapper tbUserMapper) {
        this.tbUserMapper = tbUserMapper;
    }

    public void addUser() {
        System.out.println("11111");
        System.out.println(aService == map.get("A"));
        tbUserMapper.insert(new TbUser(
                "String name", "phone",
                "String userName", new Date(), new Date()));
    }

}
